package com.cedifie.lendapp.ui.steps.impl

import android.content.Context
import com.cedifie.http.callback.StringCallback
import com.cedifie.http.model.Progress
import com.cedifie.http.model.Response
import com.cedifie.http.request.base.Request
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.steps.view.ICedUploadView
import com.lib.base.utils.log.LogHelper
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/17 13:45
 * @Annotation：图片上传 Presenter 实现
 */
class CedUploadPresenterImpl(context: Context?) : MvpPresenter<ICedUploadView>(context) {


    private var model: CedUploadModelImpl? = null
    init {
        model = CedUploadModelImpl()
    }

    /**
     * 上传图片
     */
    fun onCedUploadImageAction(imgPath: String){
        model?.onCedUploadImageAction(imgPath,object : StringCallback() {
            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                super.onStart(request)
                LogHelper.e("upload start")
            }
            override fun onSuccess(response: Response<String>?) {
                LogHelper.e("upload success ："+response?.body())
                response?.let { resp ->
                    if (resp.code() == 200){
                        val data = JSONObject(resp.body())
                        when(data.optInt("code")){
                            0 -> {//成功
                                val obj = data.optJSONObject("obj")
                                obj?.let {
                                    val imgUrl = it.optString("url")
                                    mvpView?.showCedUploadSuccess(imgUrl)
                                }
                            }
                            else -> {
                                val msg = data.optString("msg")
                                mvpView?.showCedToast(msg)
                                mvpView?.showCedUploadFailed()
                            }
                        }
                    } else {
                        mvpView?.showCedToast(response.message())
                        mvpView?.showCedUploadFailed()
                    }
                }
            }
            override fun onError(response: Response<String>?) {
                LogHelper.e("upload error： "+response?.body())
                response?.let {
                    mvpView?.showCedToast(it.message())
                }
                mvpView?.showCedUploadFailed()
            }
            override fun uploadProgress(progress: Progress) {
                LogHelper.e("uploadProgress: $progress")
                LogHelper.e("uploadProgress，total: ${progress.totalSize}")
                LogHelper.e("uploadProgress，progress: ${progress.currentSize}")
                mvpView?.showCedUploadProgress(progress.totalSize,progress.currentSize)
            }

            override fun onFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }
}